package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import utils.Log4Test;
import utils.WebDriverWrapper;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Created by sky_lady on 5/22/14.
 */
public class HomePage extends BasePage {

    public HomePage(WebDriverWrapper driver) {
        super(driver, URL);
    }

    private static final String URL = "http://rozetka.com.ua/";
    protected final By searchString = By.className("header-search-input-text");
    protected final String productFoundLocator = "//a[contains(text(), '%s' )]";
   // protected final By compareProductDetails = By.name("comparison_details");
    protected final By compareProductCatalog = By.name("comparison_catalog");
    protected final By addProductToComparison = By.name("tocomparison");

    @FindBy(how = How.XPATH, using = ".//*[@id='computers-notebooks']/div/a[1]")
    protected WebElement computersPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='phones-mp3-gps']/div/a")
    protected WebElement phonesPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='tv-photo-video']/div/a")
    protected WebElement tvPhotoPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='bt']/div/a")
    protected WebElement btPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='outdoorsman']/div/a")
    protected WebElement outDoorsPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='kids']/div/a")
    protected WebElement kidsPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='house_garden']/div/a")
    protected WebElement houseGardenPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='273290']/div/a")
    protected WebElement carGoodsPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='fatmenu_14']/div/a")
    protected WebElement ticketsPageButton;

    @FindBy(how = How.XPATH, using = ".//*[@id='shoes']/div/a")
    protected WebElement shoesPageButton;


    public WebElement getMenuItem(String name) {
        if (name == "computersPageButton")
            return computersPageButton;
        else if (name == "phonesPageButton")
            return phonesPageButton;
        else if (name == "tvPhotoPageButton")
            return tvPhotoPageButton;
        else if (name == "btPageButton")
            return btPageButton;
        else if (name == "outDoorsPageButton")
            return outDoorsPageButton;
        else if (name == "kidsPageButton")
            return kidsPageButton;
        else if (name == "houseGardenPageButton")
            return houseGardenPageButton;
        else if (name == "carGoodsPageButton")
            return carGoodsPageButton;
        else if (name == "ticketsPageButton")
            return ticketsPageButton;
        else if (name == "shoesPageButton")
            return shoesPageButton;
        return null;
    }

    public void open() {
        driver.get(URL);
    }

    public boolean searchProduct(String productName) {
        Log4Test.info("searching product: " + productName);
        driver.findElement(searchString).clear();
        driver.findElement(searchString).sendKeys(productName);
        driver.findElement(searchString).sendKeys(Keys.RETURN);
        return driver.findElements(By.xpath(String.format(productFoundLocator, productName))).size()>0;
    }

    public boolean addProductToComparison() {
        Log4Test.info("Adding product to comparison");
        driver.findElement(addProductToComparison).click();
        return driver.findElement(compareProductCatalog).isDisplayed();
    }


    public boolean checkComparison(String product1, String product2) {
        Log4Test.info("Comparision of: " + product1 + " and " + product2);
        try {
            driver.findClickableElement(compareProductCatalog).click();
            return driver.findElement(By.partialLinkText(product1)).isDisplayed() && driver.findElement(By.partialLinkText(product2)).isDisplayed();
        } catch (org.openqa.selenium.TimeoutException e) {
            Log4Test.warn(e.toString());
        }
        return false;
    }


    public boolean clickOnButton(String buttonElement) {
        Log4Test.info("click on button: " + buttonElement);
        getMenuItem(buttonElement).click();
        return getMenuItem(buttonElement).isEnabled();
    }


}

